package com.servicesystem.servicesystem.controller.ali;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.servicesystem.servicesystem.entity.AliGoodsLib;
import com.servicesystem.servicesystem.entity.vo.ali.GoodsDetails;
import com.servicesystem.servicesystem.entity.vo.ali.SearchGoodsVo;
import com.servicesystem.servicesystem.service.AliGoodsLibService;
import com.servicesystem.servicesystem.untis.HttpRequestAll;
import com.servicesystem.servicesystem.untis.R;
import com.servicesystem.servicesystem.untis.ali.AliData;
import com.servicesystem.servicesystem.untis.ali.AliSign;
import com.servicesystem.servicesystem.untis.ali.RequestUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/goods")
@RestController
public class GoodsController {

    private final HttpRequestAll httpRequestAll = new HttpRequestAll();
    @Autowired
    private AliGoodsLibService aliGoodsLibService;

    @PostMapping("search")
    public R searchGoods(@RequestBody SearchGoodsVo searchGoodsVo) {
        R result;
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("page", searchGoodsVo.getCurrent().toString());
            params.put("keyWords", searchGoodsVo.getKeyWords());
            params.put("pageSize", 12 + "");
            String signature = AliSign.getSignature("param2/1/com.alibaba.p4p/alibaba.cps.op.searchCybOffers/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("page", searchGoodsVo.getCurrent());
            objectObjectHashMap.put("keyWords", searchGoodsVo.getKeyWords());
            objectObjectHashMap.put("pageSize", 12);
            objectObjectHashMap.put("_aop_signature", signature);
            result = httpRequestAll.doPostWithParams(RequestUrl.SEARCH_URL, objectObjectHashMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    @PostMapping("detail")
    public R goodsDetails(@RequestBody GoodsDetails goodsDetails) {
        R result;
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("offerId", goodsDetails.getOfferId());
            String signature = AliSign.getSignature("param2/1/com.alibaba.product/alibaba.cpsMedia.productInfo/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("offerId", goodsDetails.getOfferId());
            objectObjectHashMap.put("_aop_signature", signature);
            result = httpRequestAll.doPostWithParams(RequestUrl.GOODS_URL, objectObjectHashMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    @GetMapping("detail/{offerId}")
    public R goodsDetails(@PathVariable(value = "offerId") String offerId) {
        R result;
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("offerId", offerId);
            String signature = AliSign.getSignature("param2/1/com.alibaba.product/alibaba.cpsMedia.productInfo/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("offerId", offerId);
            objectObjectHashMap.put("_aop_signature", signature);
            result = httpRequestAll.doPostWithParams(RequestUrl.GOODS_URL, objectObjectHashMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    @PostMapping("add-goods")
    public R saveGoods(@RequestBody AliGoodsLib aliGoodsLib) {
        String offerId = aliGoodsLib.getOfferId();
        QueryWrapper<AliGoodsLib> aliGoodsLibQueryWrapper = new QueryWrapper<>();
        aliGoodsLibQueryWrapper.eq("offer_id", offerId);
        int count = aliGoodsLibService.count(aliGoodsLibQueryWrapper);
        if (count >= 1) return R.fail("不可重复添加商品");
        new HashMap<String, List<String>>();
        Type type = new TypeToken<HashMap<String, List<String>>>() {
        }.getType();
        HashMap<String, List<String>> stringListHashMap = new Gson().fromJson(aliGoodsLib.getImage(), type);
        String arrImg = clearData(stringListHashMap.get("images").stream().map(item -> "\"" + item + "\"").collect(Collectors.toList()).toString());
        aliGoodsLib.setImage("{\"images\":" + arrImg + "}");
        String desc = clearData(aliGoodsLib.getDescription());
        aliGoodsLib.setDescription(desc);
        aliGoodsLibService.save(aliGoodsLib);
        return R.success();
    }

    public String clearData(String res) {
        List<String> arrImg = new ArrayList<>();
        Type type = new TypeToken<List<String>>() {
        }.getType();
        List<String> arrayList = new Gson().fromJson(res, type);
        arrayList.forEach(item -> {
            System.out.println(item);
            if (!"".equalsIgnoreCase(item)) {
                arrImg.add("\"" + item + "\"");
            }
        });
        return arrImg.toString();
    }

    @PostMapping
    public R getTtOrder() {

        return R.success();
    }

    @PostMapping("place-on-order")
    public R placeOnOrder() {

        return R.success();
    }
}
